package com.linln.wxapi;

import com.linln.common.constant.AdminConst;
import com.linln.common.constant.NoticeTypeConst;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.AgentContact;
import com.linln.modules.member.domain.Feedback;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.member.domain.Membernotice;
import com.linln.modules.member.service.FeedbackService;
import com.linln.modules.member.service.MemberAgentService;
import com.linln.modules.member.service.MembernoticeService;
import com.linln.modules.message.domain.MessageInfo;
import com.linln.modules.message.domain.MessageRefund;
import com.linln.modules.message.service.MessageRefundService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author hct
 * @date 2021/03/09
 */
@Controller
@RequestMapping("/wx/feedback/{area}")
public class WxApiFeedbackController {

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private MemberAgentService agentService;

    @Autowired
    private MembernoticeService noticeService;
    @Autowired
    private MessageRefundService messageRefundServiceImpl;

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @ResponseBody
    public ResultVo toDetail(@PathVariable("id") Long id) {
        Feedback feedback=feedbackService.getById(id) ;
        if(feedback==null)return ResultVoUtil.error("不存在");
        noticeService.updateIsread(NoticeTypeConst.FEEDBACK,feedback.getId());
        return ResultVoUtil.success(feedback);
     }

    /**
     * 保存添加/修改的数据
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultVo save(HttpServletRequest request,@PathVariable String area, Long memberId,Feedback feedback) {
        // 复制保留无需修改的数据
        feedback.setMemberid(memberId);
        feedback.setArea(area);
        MemberAgent memberAgent=agentService.getByArea(area);
        if(memberAgent!=null){
            feedback.setAgentid(memberAgent.getId());
         }
        feedbackService.save(feedback);
        //给管理员区域发消息
        if(memberAgent!=null &&memberAgent.getMember()!=null){
            MessageRefund message=new MessageRefund();
            message.setMemberId(memberAgent.getMember().getId());
            message.setMessageId(feedback.getId());
            message.setStatus(0);
            message.setType(NoticeTypeConst.FEEDBACK);
            message.setContent("您收到一条建议请查看");
            message.setCreateDate(new Date());
            message.setUpdateDate(new Date());
            messageRefundServiceImpl.saveMessageRefund(message);
        }
        // 保存数据
        return ResultVoUtil.SAVE_SUCCESS;
    }

}